package com.mweya;
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Question1(args);
Question2(args);
}
private static void Question1(String[] args){
System.out.println("Question 1");
char[] vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
String input = scanner.next();
try {
int number = Integer.parseInt(input);
System.out.println("'"+number+"' is"+(isPrime(number) ? "" : " not") + " prime");
} catch (NumberFormatException e) {
// Not a number, might be a char or string
if (input.length() == 1) {
// it's a char
char c = input.charAt(0);
boolean isVowel = false;
for (char v : vowels) {
if (v == c) {
isVowel = true;
}
}
System.out.println("'"+c+"' is"+ (isVowel ? "" : " not") +" a vowel");
} else {
// it's a string
System.out.println("'"+input+"' is"+ (isPalindrome(input) ? "" : " not")+" a palindrome");
}
}
}
// Yoinked from https://www.programiz.com/java-programming/examples/prime-number
private static boolean isPrime(int num) {
int i = 2;
boolean prime = false;
while (i <= num / 2) {
// condition for nonprime number
if (num % i == 0) {
prime = true;
break;
}
++i;
}
return prime;
}
// Yoinked from https://www.baeldung.com/java-palindrome
private static boolean isPalindrome(String text) {
StringBuilder reverse = new StringBuilder();
String clean = text.replaceAll("\\s+", "").toLowerCase();
char[] plain = clean.toCharArray();
for (int i = plain.length - 1; i >= 0; i--) {
reverse.append(plain[i]);
}
return (reverse.toString()).equals(clean);
}
private static void Question2(String[] args){
System.out.println("Question 2");
String input = scanner.next();
try {
int size = Integer.parseInt(input);
if (size<0) {
throw new NumberFormatException();
}
String longest = "";
String temp;
for (int i =0; i<size; i++) {
temp = scanner.next();
if (temp.length() > longest.length()) {
longest = temp;
}
}
System.out.println(longest+" is the longest name");
} catch (NumberFormatException e) {
System.out.println("Not a number / Number not positive");
}
}
}